package blockr.function;


import java.io.*;

/**
 * @author spencercjh
 */
public class Output {

    /**
     * 输出明文
     *
     * @param outputPath
     * @param allBlock
     * @param charsetName
     */
    public static void outputContent(String outputPath, byte[][][] allBlock, String charsetName) {
        StringBuilder output = new StringBuilder();
        int i = 0;
        for (byte[][] indexBlock : allBlock) {
            if (indexBlock != null) {
                boolean pieceAllZero = false;
                int endIndex = 0;
                byte[] endBlockEndPiece;
                for (byte[] indexPiece : indexBlock) {
                    /*将最后一块中的空白（byte=0）的部分去掉 循环遍历这一片如果这一片全0就退出所有循环 结束输出工作*/
                    if (i == MappedBiggerFileReader.getBlock_index()) {
                        for (byte indexByte : indexPiece) {
                            if (indexByte != 0) {
                                /*记录有效字符 有效字符仍然需要输出*/
                                endIndex++;
                            }
                        }
                        /*该片没有有效字符 全为0*/
                        if (endIndex == 0) {
                            pieceAllZero = true;
                        } else if (endIndex > 0) {
                            endBlockEndPiece = new byte[endIndex];
                            /*将有效部分复制出来 准备输出*/
                            System.arraycopy(indexPiece, 0, endBlockEndPiece, 0, endIndex);
                            try {
                                output.append(new String(endBlockEndPiece, charsetName));
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                                System.err.println("明文最后块最后片byte[]转String失败");
                            }
                        }
                    }
                    if (indexPiece != null) {
                        try {
                            output.append(new String(indexPiece, charsetName));
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                            System.err.println("明文byte[]转String失败");
                        }
                    } else {
                        break;
                    }
                    /*一整片全为空 说明后面不会再有内容了 退出输出循环*/
                    if (pieceAllZero) {
                        break;
                    }
                }
                i++;
            }
        }
        try {
            OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(outputPath), charsetName);
            out.append(output);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("输出密文文件失败");
        }
    }

    /**
     * 输出密文
     *
     * @param outputPath
     * @param allBlock
     */
    public static void output(String outputPath, byte[][][] allBlock) {
        boolean flagExit = true;
        try {
            OutputStream out = new FileOutputStream(outputPath);
            for (byte[][] indexBlock : allBlock) {
                if (indexBlock != null) {
                    for (byte[] indexPiece : indexBlock) {
                        if (indexPiece != null) {
                            flagExit = true;
                            for (byte indexByte : indexPiece) {
                                if (indexByte != 0) {
                                    flagExit = false;
                                }
                            }
                            if (flagExit) {
                                break;
                            } else {
                                out.write(indexPiece);
                            }
                        } else {
                            break;
                        }
                    }
                    if (flagExit) {
                        break;
                    }
                } else {
                    break;
                }
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.err.println("输出文件失败");
        }
    }

}
